/*
 * @Descripttion: 
 * @version: 
 * @Author: TT
 * @Date: 2023-10-26 11:26:00
 * @LastEditors: TT
 * @LastEditTime: 2023-10-27 16:28:43
 */

import 'package:flutter/material.dart';

import 'package:hzy_project_template/base_ui_config/base_ui_config_index.dart';
import 'package:hzy_project_template/config/config_index.dart';
import 'package:hzy_project_template/controllers/user_c.dart';
import 'package:hzy_project_template/models/user_model.dart';
import 'package:hzy_project_template/utils/utils_index.dart';

class MyChangeInfoV extends CommonGetXV<MyChangeInfoC> {
  MyChangeInfoV({Key? key}) : super(key: key);
  @override
  MyChangeInfoC get controller => Get.put(MyChangeInfoC());

  @override
  Widget createBody({
    required BuildContext context,
    BoxConstraints? constraints,
  }) {
    Widget body = controller.type == 0
        ? configChangeNickNameWidget(
            context: context,
          )
        : configChangeBriefWidget(
            context: context,
          );

    body = Container(
      margin: EdgeInsets.all(
        15.w,
      ),
      padding: EdgeInsets.only(
        left: 15.w,
        right: 15.w,
        bottom: 15.w,
        top: 10.w,
      ),
      decoration: BoxDecoration(
        color: HzyCommonColor().whitebackgroundColor,
        borderRadius: BorderRadius.circular(
          16.r,
        ),
      ),
      child: body,
    );

    return body;
  }

  // 配置修改昵称
  configChangeNickNameWidget({
    required BuildContext context,
  }) {
    return configChangeInfoTf(
      context: context,
      hintText: NoteLauncIdConfig.nickNameHint.tr,
      maxLength: 30,
      maxLines: 2,
    );
  }

  configChangeInfoTf({
    required BuildContext context,
    required String hintText,
    required int maxLength,
    required int maxLines,
  }) {
    Widget body = createNormaltfWidget(
      context: context,
      textEditingController: controller.textEditingController,
      decoration: InputDecoration(
        border: InputBorder.none,
        hintText: hintText,
        hintStyle: HzyTextStyle.fontMedium(
          size: 16.sp,
          color: HzyCommonColor().colplacetextcolor,
        ),
        counterStyle: HzyTextStyle.fontMedium(
          size: 16.sp,
          color: HzyCommonColor().wbgblacktextcolor,
        ),
        contentPadding: EdgeInsets.only(
          bottom: 0.w,
          top: 0.w,
        ),
      ),
      maxLength: maxLength,
      maxLines: maxLines,
      isExpanded: false,
      style: HzyTextStyle.fontMedium(
        size: 16.sp,
        color: HzyCommonColor().wbgblacktextcolor,
      ),
      cursorColor: HzyCommonColor().wbgblacktextcolor,
      onChange: (value) {
        controller.onChange(
          text: value,
        );
      },
    );

    return body;
  }

  /// 配置修改简介
  configChangeBriefWidget({
    required BuildContext context,
  }) {
    return configChangeInfoTf(
      context: context,
      hintText: NoteLauncIdConfig.fillIntrod.tr,
      maxLength: 130,
      maxLines: 4,
    );
  }

  @override
  List<Widget>? createAppBaractions() {
    return [
      GetBuilder<MyChangeInfoC>(
        builder: (controller) {
          return HzyBtnWidget(
            text: LaunchIdConfig.save.tr,
            padding: EdgeInsets.only(
              right: 10.w,
            ),
            fontColor: controller.isCanTap
                ? HzyCommonColor().wbgblacktextcolor
                : HzyCommonColor().colplacetextcolor,
            tapCall: () {
              controller.tapSave();
            },
          );
        },
      )
    ];
  }
}

class MyChangeInfoC extends CommonGetXController {
  // --------- 属性 --------- //
  bool isCanTap = false;
  int type = 0;
  String oldText = "";
  late TextEditingController textEditingController;

  // --------- 生命周期  --------- //

  @override
  configDefault() {
    type = currentGetArguments();
    String name = "";

    switch (type) {
      case 0:
        name = NoteLauncIdConfig.nickName.tr;
        textEditingController = TextEditingController(
          text: UserC.current.user?.nickName ?? "",
        );
        oldText = UserC.current.user?.nickName ?? "";
        break;
      case 1:
        name = NoteLauncIdConfig.profiles.tr;

        textEditingController = TextEditingController(text: "");
        break;
      default:
    }

    appBarTitle = "${NoteLauncIdConfig.modify.tr}$name";
  }
  // --------- 数据处理  --------- //

  // --------- 触发事件  --------- //

  tapSave() async {
    if (isCanTap) {
      UserModel userModel = UserC.current.user!;
      String text = textEditingController.text;
      if (type == 0) {
        userModel = userModel.copyWith(
          nickName: text,
        );
      } else if (type == 1) {
        userModel = userModel.copyWith(
          profiles: text,
        );
      }
      await UserC.current.updateUserInfo(
        model: userModel,
      );
      showToastMsg(
        NoteLauncIdConfig.modify.tr,
        context: Get.context,
      );
    }
  }

  onChange({
    String? text,
  }) {
    if (text != null) {
      isCanTap = text != oldText;
      update();
    }
  }
}
